package com.fenghuajueli.libbasecoreui.mvp;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;
import com.fenghuajueli.libbasecoreui.mvp.interfaces.DialogButtonListener;
import com.fenghuajueli.libbasecoreui.widget.CustomProgressDialog;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;

public class BaseActivity extends AppCompatActivity {
    CustomProgressDialog loadingDialog;
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    private View.OnClickListener backListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyStatusBarUtils.setStatusBarWhite(this, "#141416");
        if (isHideActionBar()) {
            if (getSupportActionBar() != null) {
                getSupportActionBar().hide();
            }
        }
        ARouter.getInstance().inject(this);
    }

    public boolean isHideActionBar() {
        return true;
    }

    /**
     * 显示系统等待框
     */
    public void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new CustomProgressDialog(this);
            loadingDialog.setCancelable(false);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    /**
     * 显示系统等待框
     */
    public void showLoadingDialog(String tips) {
        hideLoadingDialog();
        loadingDialog = new CustomProgressDialog(this, tips);
        loadingDialog.setCancelable(false);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    /**
     * 显示系统等待框
     */
    public void showLoadingDialog(String msg, boolean isShowProgress) {
        hideLoadingDialog();
        loadingDialog = new CustomProgressDialog(this, msg, isShowProgress);
        loadingDialog.setCancelable(false);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    /**
     * 关闭等待框
     */
    public void hideLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }


    public void setDialogProgress(int progress) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.setProgress(progress);
        }
    }


    public void showDefalutTipsDialog(String title, String content, String confirmName, String cancelName, final DialogButtonListener dialogButtonListener) {
        builder = new AlertDialog.Builder(this)
                .setMessage(content)
                .setTitle(title)
                .setPositiveButton(confirmName, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogButtonListener.onConfirm();
                    }
                }).setNegativeButton(cancelName, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogButtonListener.onCancel();
                    }
                });
        alertDialog = builder.create();
        alertDialog.show();
    }


    /**
     * 关闭提示框
     */
    public void hideTipsDialog() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    private Boolean isQuit = false;

    public void exit() {
        if (!isQuit) {
            Timer timerexit = new Timer();
            isQuit = true;
            ToastUtils.showShort("再按一次返回键回到桌面");
            TimerTask task = null;
            task = new TimerTask() {
                @Override
                public void run() {
                    isQuit = false;
                }
            };
            timerexit.schedule(task, 3000);
        } else {
            Intent backHome = new Intent(Intent.ACTION_MAIN);
            backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            backHome.addCategory(Intent.CATEGORY_HOME);
            startActivity(backHome);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideLoadingDialog();
        hideTipsDialog();
        ToastUtils.cancel();
    }

    public void setCustomActionBar(String title) {
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        View mActionBarView = LayoutInflater.from(this).inflate(R.layout.custom_action_bar_layout, null);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) {
            return;
        }
        lp.leftMargin = 0;
        lp.rightMargin = 0;
        TextView tvTitle = mActionBarView.findViewById(R.id.tvTitle);
        tvTitle.setText(title);
        ImageView ivBack = mActionBarView.findViewById(R.id.ivBack);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backListener != null) {
                    backListener.onClick(view);
                } else {
                    finish();
                }
            }
        });
        actionBar.setCustomView(mActionBarView, lp);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        Toolbar parent = (Toolbar) mActionBarView.getParent();
        parent.setContentInsetsAbsolute(0, 0);
    }

    public void setBackListener(View.OnClickListener backListener) {
        this.backListener = backListener;
    }

    public void showToast(String content) {
        ToastUtils.showShort(content);
    }


    /**
     * 检测是否升级的对话框
     */
    public void checkAppUpdate() {
        if (SwitchKeyUtils.getAppUpdateStatus()) {
            CommonTipsDialog dialog = new CommonTipsDialog(this, "您好，因产品系统更新，为保证您的正常使用，请更新至最新版本！",
                    "取消", "更新");
            dialog.setConfirmListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PublicFunction.goComment(BaseActivity.this);
                    finish();
                }
            });
            dialog.setCancelListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            dialog.setCancelable(false);
            dialog.show();
        }
    }

}
